"use strict";
const getDocData = require("./tool/getDocData.js");
const utils = require("utils");
const postQW = require("./tool/postQW.js");
const getGroupPushContent = require("./tool/getGroupPushContent.js");
exports.main = async (event, context) => {
  let { Type } = event || {};
  const currentIsTiming = Type === "Timer";
  if (!currentIsTiming) {
    let signErrResult = utils.util.checkSign(event);
    if (!utils.util.nilValue(signErrResult)) {
      return signErrResult;
    }
  }
  let [doc_err, doc_data] = await getDocData.getDocData();
  if (doc_err) {
    if (currentIsTiming) {
      await postQW.toQWPush(postQW.createContent(doc_err));
    }
    return utils.util.resErr(doc_err);
  }
  let docArr = Array.isArray(doc_data) ? doc_data : [];
  let sizuIndex = docArr.findIndex((e) => {
    return e.des.indexOf("第四组") >= 0;
  });
  if (sizuIndex < 0) {
    let msg = "没有第四组的数据啊...";
    if (currentIsTiming) {
      await postQW.toQWPush(postQW.createContent(msg));
    }
    return utils.util.resErr(new utils.BaseErr(-3101, msg));
  }
  let sizuList = docArr[sizuIndex].list;
  if (currentIsTiming) {
    let sep = "  \r\n  ";
    let [groupContent, atMobiles] = getGroupPushContent.getGroupPushContent(sizuList, sep);
    if (atMobiles && atMobiles.length > 0) {
      let notifyUsers = atMobiles.map((e) => `@${e}`).join(" ");
      groupContent = [groupContent, notifyUsers].join(sep);
    }
    let pushContent = postQW.createContent(groupContent);
    await postQW.toDingDingPush(pushContent, atMobiles);
  }
  return utils.util.resData(sizuList);
};
